package com.zjitc.Redis;

import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/12 14:35
 * @description:
 */
@Component
public class Redis {

  @Autowired
  private static Jedis jedis;

  @Autowired
  public Redis(UserDao userDao){
    jedis = new Jedis();
    List<User> list = userDao.findAllUser();
    final String key_sex_m = "key_sex_m";
    final String key_age_larger_45 = "key_age_larger_45";
    list.forEach((item)->{
      HashMap<String,String> map = new HashMap<>();
      map.put("age",Integer.toString(item.getAge()));
      map.put("sex", item.getSex());
      jedis.hmset(Integer.toString(item.getId()),map);
      if (item.getSex().equals("m")){
        jedis.sadd(key_sex_m,Integer.toString(item.getId()));
      }
      });
    Set<String> sinter = jedis.sinter(key_sex_m,key_age_larger_45);
  }
}
